package pos.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import Clases.domain.Causa;
import Clases.domain.Manifestacion;

public class JDBCManifestacionDAO implements IManifestacionDAO {

	private Connection conne;

	IUsuarioDAO udao;
	ICausaDAO cdao;

	public JDBCManifestacionDAO() {
		conne = ConnectionManager.getInstance().checkOut();
		udao = new JDBCUsuarioDAO();
		cdao = new JDBCCausaDAO();

	}

	protected void finalize() {
		ConnectionManager.getInstance().checkIn(conne);
	}

	public boolean updateAsistentes(Manifestacion m) {
		PreparedStatement stmt = null;
		boolean result = false;
		String sql = "UPDATE Manifestacion M, Causa C SET M.numAsistentes = M.numAsistentes+1 WHERE C.descripcion LIKE '"
				+ m.getManifestacionCausa().getCausaDescripcion()
				+ "' AND C.nomBarrio LIKE '"
				+ m.getManifestacionCausa().getBarrio()
				+ "' AND C.nomZona LIKE '"
				+ m.getManifestacionCausa().getZona()
				+ "' AND C.manifestacionOID = M.manifestacionOID ";

		try {
			stmt = conne.prepareStatement(sql);
			stmt.executeUpdate();

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());

		}
		try {
			if (stmt != null)
				stmt.close();
			if (stmt != null)
				stmt.close();
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
				this.finalize();
			
		}

		return result;
	}

	public List selectAllManifestacion() {

		PreparedStatement stmt = null;
		List searchResults = new LinkedList();
		ResultSet result = null;

		try {

			String sql = "SELECT* FROM Manifestacion";
			stmt = conne.prepareStatement(sql);
			result = stmt.executeQuery();

			while (result.next()) {

				Manifestacion m = new Manifestacion();
				Integer moid = result.getInt("manifestacionOID");

				m.setManifestacionCausa(cdao.selectCausasManifestacion(conne,
						moid));
				m.setManifestacionUsuarios(udao.selectAllUsuariosManifestacion(
						conne, moid));

				m.setManifestacionFecha(result.getString("fecha"));
				m.setManifestacionNumAsistentes(result.getInt("numAsistentes"));
				searchResults.add(m);
			}

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
				this.finalize();
			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}

		}
		return searchResults;
	}

	public List selectManifestacionZona(Connection conn, Integer zonaOID) {

		PreparedStatement stmt = null;
		List searchResults = new LinkedList();
		ResultSet result = null;

		try {

			String sql = "SELECT * FROM Manifestacion WHERE zonaOID  LIKE '"
					+ zonaOID + "'";
			stmt = conn.prepareStatement(sql);
			result = stmt.executeQuery();

			while (result.next()) {

				Manifestacion m = new Manifestacion();
				Integer moid = result.getInt("manifestacionOID");

				m.setManifestacionCausa(cdao.selectCausasManifestacion(conn,
						moid));
				m.setManifestacionUsuarios(udao.selectAllUsuariosManifestacion(
						conn, moid));

				m.setManifestacionFecha(result.getString("fecha"));
				m.setManifestacionNumAsistentes(result.getInt("numAsistentes"));
				searchResults.add(m);
			}

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {

			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}

		}
		return searchResults;
	}
}
